﻿<?php // no direct access

defined('_JEXEC') or die('Restricted access');
jimport( 'joomla.application.component.view');

// montre les prochaines distributions, tout abo confondu
class amapViewliste_pointageldb_sans_detail extends JView	{
	var $amap;
	function display($tpl = null)	{
		$mainframe =& JFactory::getApplication();
		$document =& JFactory::getDocument();
		$pathway  =& $mainframe->getPathway();
		// Adds parameter handling $params = $mainframe->getParams();
		if ($this->amap->adLoggue){
			if ($this->amap->adExiste)	{
				$date_a_imprimer= $_GET['Date'];
				$format = $_GET['extension'];
				
				echo "<h1>AMAP de Luy de Béarn - Liste de pointage de la livraison du ".fdate($date_a_imprimer)." </h1><br>";

				//~ Requête récupérant les données
				$query = "SELECT T12.NomAbonnement as NomAbonnement, T34.Nom as Nom, T34.Prenom as Prenom, T34.Fixe as Fixe, T34.Mobile as Mobile FROM
					( SELECT T1.Date, T2.Nom, T1.NomAbonnement as NomAbonnement, T2.Produit, T2.Statut FROM amap_jourdistributionabonnement as T1 JOIN amap_abonnement as T2
						ON T2.Nom = T1.NomAbonnement
						WHERE (T2.Statut = 'Ouvert' OR T2.Statut = 'Complet')
							AND T1.Date = '".$date_a_imprimer."')
						as T12 JOIN
					( SELECT T3.Nom as Nom, T3.Prenom as Prenom, T3.Fixe as Fixe, T3.Mobile as Mobile, T4.NomAbonnement,T4.Statut FROM amap_adherent as T3 JOIN amap_abonnementadherent as T4
						ON T3.IDA = T4.IDA )
						as T34
					ON T12.Nom = T34.NomAbonnement WHERE T34.Statut='Abonne' 
					ORDER BY T34.Nom";
				$database = &JFactory::getDBO();
				$database->setQuery($query);
				$database->query(); if ($database->getErrorNum()) {echo 'Error ' . $database->getErrorNum() . ': ' . $database->getErrorMsg().'<br />';} 
				if ($database->getNumRows() != 0) { 
					$abonnements = $database->loadAssocList(); 
				} else { echo "pas d'abonnement en cours"; return $data;}

				//~ Traitement des données de la requête pour une remise en forme exploitable pour l'affichage
				$tab = array();$tab_utf = array();$tab_htm = array();
				foreach ($abonnements as $abo)	{
					$n = FormatNomsAdh($abo["Nom"], $abo["Prenom"]);
					$contrats[$abo["NomAbonnement"]] = 1;
					$adherents[$n] = FormatPhone($abo["Fixe"]).'<br/>'.FormatPhone($abo["Mobile"]);
					$tab[$abo["NomAbonnement"]][$n] = 1;
				}
				ksort($contrats);
				
				$largeurcolonnenom = 80;
				$largeurcolonnecontrat = (672-($largeurcolonnenom*2)-10)/sizeof($contrats);
				$contenu = '<div style="font-size: 20px; text-align: center;">Livraisons du <b style="color: red;">'.fdate($date_a_imprimer).'</b></div>';
				$contenu .= '<table border="1" width="100%"><tr><td colspan="2" width="160"></td>';
				$col = 2; $lig = 1;
				$tab_htm[$lig][1] = "";	$tab_utf[$lig][1] = "";
				$tab_htm[$lig][2] = "--Téléphones--";	$tab_utf[$lig][2] = "";
				foreach ($contrats as $kc=>$contrat)	{
					$col++;
					$contenu .= '<td align="center" valign="center" style="background-color: #E6E6E7; font-size: 10px;" width="'.$largeurcolonnecontrat.'"><b>'.$kc.'</b></td>';
//20140618BM
$nom_court=$kc;

//$nom_court=substr($nom_court,22);
$nom_court=substr($nom_court,1+strrpos($nom_court,":"));
//$nom_court=substr($nom_court,1,strrpos($nom_court,"-"));
//$kc = $nom_court;
if(substr_count($nom_court,"-") > 1)
{$nom_court=substr($nom_court,1,strpos($nom_court,"-")-1);
}
$kc = $nom_court;		
			$tab_htm[$lig][$col] = $kc;		$tab_utf[$lig][$col] = utf8_encode($kc);
				}
				$contenu .= '</tr>';
				foreach ($adherents as $ka=>$adherent)	{
					$lig++; $col = 2;
					$contenu .= '<tr style="'.(($lig%2)?'':'background-color: #E6E6D7;').'">';
					$contenu .= '<td align="center" height="30" style="font-size: 10px;" width="'.$largeurcolonnenom.'">'.$ka.'</td>';
					$contenu .= '<td align="center" style="font-size: 10px;" width="'.$largeurcolonnenom.'">'.$adherent.'</td>';
					$tab_htm[$lig][1] = $ka;			$tab_utf[$lig][1] = utf8_encode(preg_replace("/<br[^>]*>\s*\r*\n*/is", "-", $ka));
					$tab_htm[$lig][2] = $adherent;		$tab_utf[$lig][2] = utf8_encode(preg_replace("/<br[^>]*>\s*\r*\n*/is", "-", $adherent));
					foreach ($contrats as $kc=>$contrat)	{
						$col++;
						$contenu .= '<td align="center" style="font-size: 24px; vertical-align: middle;">';
						if (isset($tab[$kc][$ka]))	{
							$contenu .= 'X';
							$tab_htm[$lig][$col] = "X";			$tab_utf[$lig][$col] = "X";
						}
						else	{
							$tab_htm[$lig][$col] = "&nbsp;";	$tab_utf[$lig][$col] = "";
						}
						$contenu .= '</td>';
					}
					$contenu .= '</tr>';
				}
				$contenu .= '</table>';
				
				if($format=='html')	{exportHTML($tab_htm);} 
				if ($format=='tableur')	{exportExcel($tab_utf);}
//20140831BM manque parémètre ajouté 'contrat'
				if($format=='pdf')	exportPDF($tab_htm, $date_a_imprimer,'');


			}
		}
	}
}

function nbJours($debut, $fin) {
	global $tab_col;        //60 secondes X 60 minutes X 24 heures dans une journée
    $nbSecondes= 60*60*24;

    $debut_ts = strtotime($debut);
    $fin_ts = strtotime($fin);
    $diff = $fin_ts - $debut_ts;
    return round($diff / $nbSecondes);
}
	
?>
